package com.ruoyi.tongban.utils;

import com.ruoyi.common.utils.http.HttpUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 微信相关Api
 * @Author caiyanshan
 * @Date 2021/3/10 14:13
 * @Param
 * @Return
 */
public class WechatApi {
  
  private final static Logger logger = LoggerFactory.getLogger(WechatApi.class);
  
  private static String createwxaqrcodeUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=";//获取applets永久二维码地址
  
  
  /**
   * @Description 获取小程序二维码图片
   * @Author caiyanshan
   * @Date 2020/10/16 13:46
   * @Param []
   * @Return java.nio.Buffer
   */
  public static byte[] getQRImage(Long institutionId, Long classId){
    String url = createwxaqrcodeUrl + WechatUtil.getAccessToken();
    JSONObject json = new JSONObject();
    StringBuffer paramBuffer = new StringBuffer();
    paramBuffer.append("pages/reg/index?institutionId=");
    paramBuffer.append(institutionId.toString());
    if(classId != null) {
      paramBuffer.append("&");
      paramBuffer.append("classId=" + classId);
    }
    try {
      json.put("path", paramBuffer.toString());
    }catch(Exception e){
      logger.error("create qr code is error, the error is : " + e.getMessage());
    }
    return HttpUtils.requestWXHandleQRCode(url, json.toString());
  }
}
